<section class="content-header">
	<h1>
		设备管理 <small>列表</small>
	</h1>
	<ol class="breadcrumb">
		<li><a href="${basePath}"><i class="fa fa-dashboard"></i> 首页</a></li>
		<li><a href="#">系统管理</a></li>
		<li class="active">设备管理</li>
	</ol>
</section>

<section class="content">
	<div class="row">
		<div class="col-md-2">

            <!-- Profile Image -->
            <div class="box box-primary">
                <div class="box-body box-profile">
                    <div id="tree"></div>
                </div>
                <!-- /.box-body -->
            </div>
            <!-- /.box -->
        </div>
	
		<div class="col-md-6">
			<div class="box box-primary">
				<!-- /.box-header -->
				<div class="dataTables_filter" id="searchDiv">
					<input type="hidden"  name="levelCode" value="-1" id="levelCode" operator="like"/>
					<!--查询条件-->
					<input type="search" placeholder="请输入名称" title="名称" name="name" class="form-control" id="name" operator="like" likeOption="true">
					<div class="btn-group">
						<button type="button" class="btn btn-primary" data-btn-type="search">查询</button>
						<button type="button" class="btn btn-default" data-btn-type="reset">重置</button>
					</div>
					<div class="btn-group">
						<button type="button" class="btn btn-default" data-btn-type="edit">编辑</button>
						<button type="button" class="btn btn-default" data-btn-type="delete">删除</button>
					</div>
				</div>
				<div class="box-body">
					<table id="device_table" class="table table-bordered table-striped table-hover">
					</table>
				</div>
			</div>
		</div>
		<div class="col-md-4">
			<!-- Profile Image -->
			<div class="box box-primary">
				<!-- /.box-header -->
				<div class="dataTables_filter" style="margin: 10px 10px 0 0" id="searchDiv_sensorFunc">
					<input type="hidden"  name="deviceId" value="-1" id="deviceId"/>
					<div class="btn-group">
						<button type="button" class="btn btn-default" data-btn-type="addSensor">添加</button>
						<button type="button" class="btn btn-default" data-btn-type="editSensor">编辑</button>
						<button type="button" class="btn btn-default" data-btn-type="delSensor">删除</button>
					</div>
				</div>
				<div class="box">
					
					<!-- /.box-header -->
					<div class="box-body no-padding">
						<table id="sensor_table" class="table table-bordered table-striped table-hover"></table>
					
						<!-- <table class="table table-striped">
							<tbody>
								<tr>
									<th style="width: 20px">#</th>
									<th>传感器名称</th>
									<th>IO口</th>
									<th>取值</th>
									<th style="width: 50px">单位</th>
								</tr>
								<tr>
									<td>1.</td>
									<td>温度传感器</td>
									<td>3</td>
									<th>1</th>
									<td><span class="badge bg-red">0c</span></td>
								</tr>
								<tr>
									<td>2.</td>
									<td>二氧化碳</td>
									<td>9</td>
									<td>0</td>
									<td><span class="badge bg-yellow">pm</span></td>
								</tr>
								<tr>
									<td>3.</td>
									<td>风速</td>
									<td>6</td>
									<td>1</td>
									<td><span class="badge bg-light-blue">30%</span></td>
								</tr>
							</tbody>
						</table> -->
					</div>
				</div>
				<!-- /.box-body -->
			</div>
			<!-- /.box -->
		</div>
	</div>
</section>

<script>
	var deviceTable,sensorTable, winId = "deviceWin", swinId="sensorWin";
	
	
	var deviceObj = {
			loadData: function (){
				var device_config = {
						//定义选中行事件
						rowClick : function(row, isSelected) {
							$("#deviceId").val(isSelected ? row.id : "-1");
							$("#deviceName").remove();
							if (isSelected)
								$("#searchDiv_sensorFunc").prepend("<h5 id='deviceName' class='pull-left'>&nbsp;&nbsp;&nbsp;&nbsp;传感器管理 【" + row.type + "】</h5>");
							sensorTable.reloadData();
						},
						//数据加载后，默认选中第一行
						loadComplete : function() {
							deviceTable.selectFirstRow(true);
						}
					}
				deviceTable = new CommonTable("device_table", "device", "searchDiv", device_config);
				sensorTable = new CommonTable("sensor_table","sensor_list", "searchDiv_sensorFunc", null);
				//初始化菜单树
				this.initTree(0);
				sensorTable.reloadData();
			},
			
			loadSensor: function () {//加载传感器数据
				var selectNodeId = 0;
	            var deviceData = null;
	            var rowId = deviceTable.getSelectedRowId();
	            if (!rowId)
	                return;
	            /* ajaxPost(basePath + "/function/listTree/" + rowId, null, function (data) {
	                deviceData = data;
	            }); */
	            alert(rowId);
			},
			
			initTree: function (selectNodeId) {
	            var self = this;
	            var treeData = null;
	            ajaxPost(basePath + "/org/treeData", null, function (data) {
	                treeData = data;
	            });
	            $("#tree").treeview({
	                data: treeData,
	                showBorder: true,
	                expandIcon: "glyphicon glyphicon-stop",
	                collapseIcon: "glyphicon glyphicon-unchecked",
	                levels: 3,
	                onNodeSelected: function (event, data) {
	                	console.info(data);
	                	$("#levelCode").val(data.levelCode);
	                	deviceTable.reloadData();
	                }
	            });
	            if (treeData.length == 0)
	                return;
	            //默认选中第一个节点
	            selectNodeId = selectNodeId || 0;
	            $("#tree").data('treeview').selectNode(selectNodeId);
	            $("#tree").data('treeview').expandNode(selectNodeId);
	            $("#tree").data('treeview').revealNode(selectNodeId);
	        }
	}
	
	
	
	
	$(function() {

		deviceObj.loadData();

		
		//绑定按钮事件
		$("button[data-btn-type]").click(
				function() {
					var action = $(this).data("btn-type");
					var rowId = deviceTable.getSelectedRowId();
					switch (action) {
					case "edit":
						if (!rowId) {
							modals.info('请选择要编辑的行');
							return false;
						}
						modals.openWin({
							winId : winId,
							title : '编辑设备【'+ deviceTable.getSelectedRowData().name + '】',
							width : '900px',
							url : basePath + "/device/edit?id=" + rowId
						});
						break;
					case 'delete':
						if (!rowId) {
							modals.info('请选择要删除的行');
							return false;
						}
						modals.confirm("是否要删除该行数据？", function() {
							ajaxPost(basePath + "/device/delete/" + rowId, null, function(data) {
								if (data.success) {
									deviceTable.reloadRowData();
								} else {
									modals.info("设备数据被引用，不可删除！");
								}
							});
						})
						break;
						
					case "addSensor":
						if (!rowId) {
							modals.info('请选择设备');
							return false;
						}
						modals.openWin({
							winId : swinId,
							title : '编辑传感器【'+ deviceTable.getSelectedRowData().name + '】',
							width : '900px',
							url : basePath + "/device/sensorAdd?id=" + rowId
						});
						break;
						
					case "editSensor":
						var sensorId = sensorTable.getSelectedRowId();
						if (!sensorId) {
							modals.info('请选择传感器');
							return false;
						}
						modals.openWin({
							winId : swinId,
							title : '编辑传感器【'+ sensorTable.getSelectedRowData().name + '】',
							width : '900px',
							url : basePath + "/device/sensorEdit?id=" + sensorId
						});
						break;
						
					case "delSensor":
						var sensorId = sensorTable.getSelectedRowId();
						if (!sensorId) {
							modals.info('请选择要删除的行');
							return false;
						}
						modals.confirm("是否要删除该行数据？", function() {
							ajaxPost(basePath + "/device/deleteSensor/" + sensorId, null, function(data) {
								if (data.success) {
									sensorTable.reloadRowData();
								} else {
									modals.info("设备数据被引用，不可删除！");
								}
							});
						})
						break;
						
					}
				})
	})

	
	
	
	
	function fnRenderDept(value) {
		if (value) {
			var ret = value;
			ajaxPost(basePath + "/org/show/" + value, null, function(data) {
				ret = data.data;
			})
			return ret;
		} else
			return value;
	}
</script>